(function() {
    angular.module('app').factory('productBasicService', basicService);

    basicService.$inject = ['$rootScope'];

    function basicService($rootScope) {

        var monitorBrandCurIndex = -1; //品牌详情的序号

        var monitorShopCurIndex = -1; //店铺详情的序号

        var monitorDataCurIndex = -1; //日监控所在的序号

        return {
            getShopBasic: _getShopBasic,
            shopBasicInit: _shopBasicInit,

            getBrandBasic: _getBrandBasic,
            brandBasicInit: _brandBasicInit,

            getMonitorData: _getMonitorData,
            monitorDataInit: _monitorDataInit
        };

        function _getBrandBasic(event, index) {

            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { brandname: vm.table.products[index].brandName });
                return;
            }
            // 清除店铺详情
            if (monitorShopCurIndex != -1) {
                console.log("monitorShopCurIndex:" + monitorShopCurIndex);
                vm.table.products.splice(monitorShopCurIndex + 1, 1);
                index = index > monitorShopCurIndex ? index - 1 : index;
                monitorShopCurIndex = -1;
            }
            //清楚日监控数据
            if (monitorDataCurIndex != -1) {
                console.log("monitorDataCurIndex:" + monitorDataCurIndex);
                vm.table.products.splice(monitorDataCurIndex + 1, 1);
                index = index > monitorDataCurIndex ? index - 1 : index;
                monitorDataCurIndex = -1;
            }

            //monitorBrandCurIndex 已存在
            if (monitorBrandCurIndex == index) {
                vm.table.products.splice(index + 1, 1);
                monitorBrandCurIndex = -1;
                return;
            }

            // 清空之前的数据
            if (monitorBrandCurIndex >= 0) {
                vm.table.products.splice(monitorBrandCurIndex + 1, 1);
            }

            if (monitorBrandCurIndex >= 0 && index > monitorBrandCurIndex) {
                index--;
            }

            vm.table.products.splice(index + 1, 0, {
                monitorDetail: true,
                brandname: vm.table.products[index].brandName,
            });
            monitorBrandCurIndex = index;
        }


        function _getShopBasic(event, index) {
            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { uid: vm.table.products[index].uid });
                return;
            }

            //清楚品牌详情
            if (monitorBrandCurIndex != -1) {
                console.log("monitorBrandCurIndex:" + monitorBrandCurIndex);
                vm.table.products.splice(monitorBrandCurIndex + 1, 1);
                index = index > monitorBrandCurIndex ? index - 1 : index;
                monitorBrandCurIndex = -1;
            }
            //清楚日监控数据
            if (monitorDataCurIndex != -1) {
                console.log("monitorDataCurIndex:" + monitorDataCurIndex);
                vm.table.products.splice(monitorDataCurIndex + 1, 1);
                index = index > monitorDataCurIndex ? index - 1 : index;
                monitorDataCurIndex = -1;
            }

            //monitorShopCurIndex 已存在
            if (monitorShopCurIndex == index) {
                vm.table.products.splice(index + 1, 1);
                monitorShopCurIndex = -1;
                return;
            }

            // 清空之前的数据
            if (monitorShopCurIndex >= 0) {
                vm.table.products.splice(monitorShopCurIndex + 1, 1);
            }

            if (monitorShopCurIndex >= 0 && index > monitorShopCurIndex) {
                index--;
            }

            vm.table.products.splice(index + 1, 0, {
                monitorDetail: true,
                uid: vm.table.products[index].uid,
            });
            monitorShopCurIndex = index;
        }


        function _getMonitorData(event, index) {
            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { targetId: vm.table.products[index].numberId, type: 'product' });
                return;
            }
            // 清除店铺详情
            if (monitorShopCurIndex != -1) {
                console.log("monitorShopCurIndex:" + monitorShopCurIndex);
                vm.table.products.splice(monitorShopCurIndex + 1, 1);
                index = index > monitorShopCurIndex ? index - 1 : index;
                monitorShopCurIndex = -1;
            }
            //清楚品牌详情
            if (monitorBrandCurIndex != -1) {
                console.log("monitorBrandCurIndex:" + monitorBrandCurIndex);
                vm.table.products.splice(monitorBrandCurIndex + 1, 1);
                index = index > monitorBrandCurIndex ? index - 1 : index;
                monitorBrandCurIndex = -1;
            }

            //monitorDataCurIndex 已存在
            if (monitorDataCurIndex == index) {
                vm.table.products.splice(index + 1, 1);
                monitorDataCurIndex = -1;
                return;
            }

            // 清空之前的数据
            if (monitorDataCurIndex >= 0) {
                vm.table.products.splice(monitorDataCurIndex + 1, 1);
            }

            if (monitorDataCurIndex >= 0 && index > monitorDataCurIndex) {
                index--;
            }

            vm.table.products.splice(index + 1, 0, {
                monitorDetail: true,
                targetId: vm.table.products[index].numberId,
            });
            monitorDataCurIndex = index;
        }



        function _shopBasicInit() {
            monitorShopCurIndex = -1;
        }

        function _brandBasicInit() {
            monitorBrandCurIndex = -1;
        }

        function _monitorDataInit() {
            monitorDataCurIndex = -1;
        }
    }

})();
